package com.ht.api.task;

import javax.sql.DataSource;

import com.ht.api.db.Db;
import com.ht.api.db.DbConfig;
import com.ht.api.db.Entity;
import com.ht.api.db.StreamLoadConfig;
import com.ht.api.db.task.TaskHandler;
import com.ht.api.db.util.HtUtil;
import com.ht.api.service.TagService;

import cn.hutool.json.JSONObject;
import com.ht.api.util.NoticeUtil;

public class TagInitTask implements TaskHandler {

	@Override
	public String getName() {
		return "刷新标签";
	}

    @Override
    public boolean handler(JSONObject requestParam, String groupId, long taskId) {

        long tenantId = requestParam.getLong("tenant_id");

        DataSource ds = DbConfig.tenantDSMap.get(tenantId);
        StreamLoadConfig config = DbConfig.useTenantSLC(tenantId);

        String tagId = requestParam.getStr("tag_id");
        String userid = requestParam.getStr("user_id");

        Entity tag = Db.createSqlParser("tag").where("main_id", tagId).findOne(ds);

        TagService.refreshTag(ds, config, tag);

        NoticeUtil.send( "标签["+tag.getStr("tag_name")+"]已初始化成功！", 1, userid);

        tag.set("init_status", 2);
        Db.insert(ds, tag);

        HtUtil.createLog(ds, tagId, "初始化完成！");

        return true;
    }
}
